package com.think.archive.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.think.archive.model.SysUser;
import com.think.archive.service.TokenService;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service("TokenService")
public class TokenServiceImpl implements TokenService {
    public String getToken(SysUser user) {
        String token="";
//        Calendar cal = Calendar.getInstance();
//        cal.add(Calendar.HOUR,12);
        token= JWT.create().withAudience(user.getId()+"")// 将 user id 保存到 token 里面
                .sign(Algorithm.HMAC256(user.getUsername()));// 以 username 作为 token 的密钥
        return token;
    }

    public String getToken(SysUser user,Date date) {
        String token="";
        token= JWT.create().withExpiresAt(date).withAudience(user.getId()+"")// 将 user id 保存到 token 里面
                .sign(Algorithm.HMAC256(user.getUsername()));// 以 username 作为 token 的密钥
        return token;
    }
}
